package com.voxel.sense.infomsg.module.common;

import android.text.TextUtils;
import android.view.View;

import com.example.com.common.app.BViewActivity;
import com.example.com.common.app.BaseContract;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.EmptyView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.voxel.sense.infomsg.R;

/**
 * Created by ChenYiYao on 2018/7/30.
 * 封装基类的recyclerView
 * 继承该类必须赋值empty_view和refresh_layout控件才能调用其中的方法
 */
public abstract class VRecyclerActivity<T extends BaseContract.Presenter, M> extends
        BViewActivity<T> implements BaseContract.RecyclerView<T, M> {

    protected EmptyView empty_view;//占位的空界面布局
    protected RefreshLayout refresh_layout;//下拉刷新头

    @Override
    protected void initWidget() {
        super.initWidget();
        empty_view = findViewById(R.id.empty_view);
        refresh_layout = findViewById(R.id.refresh_layout);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEven() {
        super.initEven();
        //空界面的刷新按钮
        empty_view.addRefreshBtnEvent(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                empty_view.triggerOk();
                boolean b = refresh_layout.autoRefresh();
                //刷新过于频繁会导致失败，不会回调刷新方法
                if (!b) {
                    showEmptyView("",true);
                }
            }
        });
    }

    @Override
    public void showEmptyView(String desc, boolean flag) {
        if (refresh_layout == null) {
            return;
        }
        empty_view.setRefreshBtnVisible(flag);
        boolean b = getRecyclerViewAdapter().getItemCount() > 0;
        if (!b && !TextUtils.isEmpty(desc)) {
            empty_view.triggerError(desc);
        } else if (b) {
            //发生错误，列表有数据，不能显示空界面
            super.showError(desc);
        } else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerEmpty();
        }
    }

    @Override
    public void onShowNoMore(boolean b) {
        if (refresh_layout != null) {
            refresh_layout.setNoMoreData(b);
        }
    }

    @Override
    public void onRefreshFinish() {
        if (refresh_layout != null)
            refresh_layout.finishRefresh();

        if (empty_view != null)
            empty_view.triggerOkOrEmpty(getRecyclerViewAdapter().getItemCount() > 0 ? true : false);
    }

    @Override
    public void onLoadMoreFinish() {
        if (refresh_layout != null)
            refresh_layout.finishLoadMore();

        if (empty_view != null)
            empty_view.triggerOkOrEmpty(getRecyclerViewAdapter().getItemCount() > 0 ? true : false);
    }

    @Override
    public void onAdapterDataChanged() {

    }


    public void setEmpty_view(EmptyView empty_view) {
        this.empty_view = empty_view;
    }

    public void setRefresh_layout(RefreshLayout refresh_layout) {
        this.refresh_layout = refresh_layout;
    }
}
